package com.gzy.literature.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.dao.UserViewsDao;
import com.gzy.literature.entity.UserViews;
import com.gzy.literature.service.UserViewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;


@Service("userViewsService")
public class UserViewsServiceImpl implements UserViewsService {
    @Autowired
    private UserViewsDao userViewsDao;
    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象，若不存在则返回null
     */
    @Override
    public UserViews queryById(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("User ID must not be null");
        }
        return userViewsDao.queryById(userId);
    }
    /**
     * 分页查询
     *
     * @param userViews  筛选条件，可以为null表示无条件查询
     * @param pageNum    页码，从1开始，必须为正整数
     * @param pageSize   页面大小，必须为正整数
     * @return 查询结果封装在PageInfo中
     */
    @Override
    public PageInfo<UserViews> queryEll(UserViews userViews, int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            throw new IllegalArgumentException("Page number and page size must be positive integers");
        }
        PageHelper.startPage(pageNum, pageSize);

        List<UserViews> userViewsList = userViewsDao.queryEll(userViews);

        PageInfo<UserViews> pageInfo = new PageInfo<>(userViewsList);
        return pageInfo;
    }
    /**
     * 新增数据
     *
     * @param userViews 实例对象
     * @return 实例对象
     */
    @Override
    public UserViews insert(UserViews userViews) {
        if (userViews == null) {
            throw new IllegalArgumentException("User Views object must not be null");
        }
        userViewsDao.insert(userViews);
        return userViews;
    }
    /**
     * 修改数据
     *
     * @param userViews 实例对象
     * @return 修改后的实例对象（通常是从数据库中重新查询得到的）
     */
    @Override
    public UserViews update(UserViews userViews) {
        if (userViews == null || userViews.getUserId() == null) {
            throw new IllegalArgumentException("User Views object and user ID must not be null");
        }
        userViewsDao.update(userViews);
        return this.queryById(userViews.getUserId());
    }
    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("User ID must not be null");
        }
        return userViewsDao.deleteById(userId) > 0;
    }
}